using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;

public class AssetBuilder : BuildScript
{
    public bool forceUpdate = false;
    private ShaderAssetCollect m_ShaderAssetCollect;
    LuaAssetCollect m_LuaAssetCollect;

    AssetCollection.CollectStatus collectStatus;
    public AssetBuilder()
    {
        m_ShaderAssetCollect = new ShaderAssetCollect(GameUtility.EditorShaderPath);
        m_LuaAssetCollect = new LuaAssetCollect(GameUtility.EditorLuaPath);
    }
    [MenuItem("Tools/Bundle/BuildAndroidAsset")]

    public static void BuildAndroidAsset()
    {
        BuildLuaVersion(BuildTarget.Android);
    }
    [MenuItem("Tools/Bundle/BuildWindowsAsset")]
    public static void BuildWindowsAsset()
    {
        BuildLuaVersion(BuildTarget.StandaloneWindows64);
    }
    BuildTarget _target;
    public static void BuildLuaVersion(BuildTarget target)
    {
        AssetBuilder luaBuilder = new AssetBuilder();
        luaBuilder._target = target;
        luaBuilder.StartBuild(Path.Combine(Application.dataPath, GameUtility.EditorAssetBundlePath));

        luaBuilder.GenBundle();
    }
    List<AssetBundleBuild> builds = new List<AssetBundleBuild>();

    public override void GenBundle()
    {
        if (collectStatus != AssetCollection.CollectStatus.Ended)
        {
            return;
            // m_ShaderAssetCollect.collectStatus = AssetCollection.CollectStatus.Ended;
        }

        var out_put = $"{Application.dataPath}/{GameUtility.EditorOutPutPath}";
        GameUtility.IfExitsCreate(out_put);
        BuildPipeline.BuildAssetBundles(out_put, builds.ToArray(), BuildAssetBundleOptions.None, _target);
    }

    protected override void CollectAssetBuild()
    {
        m_ShaderAssetCollect.StartCollect();
        collectStatus = m_ShaderAssetCollect.collectStatus;
        if (collectStatus != AssetCollection.CollectStatus.Ended)
        {
            Debug.LogError("ShaderAssetCollect    Failed !!!");
            return;
        }
        builds.AddRange(m_ShaderAssetCollect.GetAssetBuild());
        m_LuaAssetCollect.StartCollect();
        collectStatus = m_LuaAssetCollect.collectStatus;
        if (collectStatus != AssetCollection.CollectStatus.Ended)
        {
            Debug.LogError("LuaAssetCollect    Failed !!!");
            return;
        }
        builds.AddRange(m_LuaAssetCollect.GetAssetBuild());
    }

    public static string GetPlatformName(BuildTarget target)
    {
        switch (target)
        {
            case BuildTarget.StandaloneWindows:
                return "Windows";
            case BuildTarget.Android:
                return "Android";
            default:
                return "Windows";
        }
    }
}
